package com.android.ExpandListView;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandListView extends ExpandableListActivity {

	
	private List <String> group;
	private List<List<String>> child;

	private void initialData(){  
		group = new ArrayList<String>();  
		child = new ArrayList<List<String>>();  
		addInfo("griffinshi", new String[]{"13776117119","man","Jiangsu"});
		addInfo("lancewu",new String[]{"1321134","man","Taiwan"});  
		addInfo("kandyli",new String[]{"12345"});  
	}  
	private void addInfo(String p,String[] c){  
		group.add(p);  
		List<String> item = new ArrayList<String>();  
		for(int i=0;i<c.length;i++){  
		  item.add(c[i]);  
		}  
		child.add(item);  
	}  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.main);
		initialData();
		setListAdapter(new InfoDetailsAdapter(this));
	}
	
	public class InfoDetailsAdapter extends BaseExpandableListAdapter {  
		Activity activity;  
		public InfoDetailsAdapter(Activity a){  
		       activity = a;  
		}
		//child method stub 
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return child.get(groupPosition).get(childPosition);  
		}
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition; 
		}
		@Override
		public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {  
			// TODO Auto-generated method stub  
			String string = child.get(groupPosition).get(childPosition);  
			return getGenericView(string);  
		}  
		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return child.get(groupPosition).size();
		}
		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return group.get(groupPosition);
		}
		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return group.size();
		}
		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			String string = group.get(groupPosition);  
			return getGenericView(string);
		}
		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}
		
		//View stub to create Group/Children 's View  
		public TextView getGenericView(String s) {  
		   // Layout parameters for the ExpandableListView  
		   AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);  
		   TextView text = new TextView(activity);  
		   text.setLayoutParams(lp);  
		   // Center the text vertically  
		   text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
		   // Set the text starting position  
		   text.setPadding(36, 0, 0, 0);  
		   text.setText(s);  
		   return text;
		}
	}//end of InfoDetailsAdapter
}//end of ExpandListView